﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aga.Controls.Tree;
using Microsoft.Win32;
using System.Collections;
using System.Collections.ObjectModel;
using System.Reflection;

namespace DienLib.PopUp
{
    class WatchModel : ITreeModel
    {
        object initobj = null;
        public WatchModel(object initObject)
        {
            initobj = initObject;
        }

        public IEnumerable GetChildren(object parent)
        {
            if (parent == null)
            {
                yield return new WatchKey()
                {
                    Name = initobj.ToString(),
                    Value = "HIC",
                    Type = initobj.GetType()
                };
            }
            else if (parent.GetType() != typeof(PropertyInfo))
            {   // Object or FieldInfo or MethodInfo or MemberInfo
                // Now implement for Object first
                yield return new WatchKey()
                {
                    Name = parent.ToString(),
                    Value = "HU",
                    Type = initobj.GetType()
                };
            }
            else // PropertyInfo 
            {
                var key = parent as PropertyInfo;
                foreach (PropertyInfo pinfo in key.PropertyType.GetProperties()) // Lay pinfo neu ton tai thi tra ve Subkey la pinfo do
                {
                    yield return new WatchKey()
                    {
                        Name = pinfo.Name,
                        Value = pinfo.GetValue(initobj, null),
                        Type = pinfo.PropertyType
                    };
                }

                /*
                foreach (var name in key.GetValueNames())
                {
                    yield return new RegValue()
                    {
                        Name = name,
                        Data = key.GetValue(name),
                        Kind = key.GetValueKind(name)
                    };
                }*/
            }
        }

        public bool HasChildren(object parent)
        {
            return parent.GetType().GetProperties().Length > 0;
        }
    }

    /*public class WatchKey
    {
        private readonly ObservableCollection<WatchKey> _children = new ObservableCollection<WatchKey>();
        public ObservableCollection<WatchKey> Children
        {
            get { return _children; }
        }

        public string Name { get; set; }

        public int Id { get; set; }

        static int _i;
        public WatchKey()
        {
            Id = ++_i;
        }

        public override string ToString()
        {
            return Name;
        }
    }*/

    public struct WatchKey
    {
        public string Name { get; set; }
        public object Value { get; set; }
        public object Type { get; set; }
    }

    public struct RegValue
    {
        public string Name { get; set; }
        public object Data { get; set; }
        public RegistryValueKind Kind { get; set; }
    }
}




#if false // This is for Registry Testing. Still a little bit slower than the main one here : http://www.codeproject.com/Articles/30721/WPF-TreeListView-Control
namespace DienLib.PopUp
{
    class WatchModel : ITreeModel
    {
        public IEnumerable GetChildren(object parent)
        {
            var key = parent as RegistryKey;
            if (parent == null)
            {
                yield return Registry.ClassesRoot;
                yield return Registry.CurrentUser;
                yield return Registry.LocalMachine;
                yield return Registry.Users;
                yield return Registry.CurrentConfig;
            }
            else if (key != null)
            {
                foreach (var name in key.GetSubKeyNames())
                {
                    RegistryKey subKey = null;
                    try
                    {
                        subKey = key.OpenSubKey(name);
                    }
                    catch
                    {
                    }
                    if (subKey != null)
                        yield return subKey;
                }

                foreach (var name in key.GetValueNames())
                {
                    yield return new RegValue()
                    {
                        Name = name,
                        Data = key.GetValue(name),
                        Kind = key.GetValueKind(name)
                    };
                }
            }
        }

        public bool HasChildren(object parent)
        {
            return parent is RegistryKey;
        }
    }

    public struct RegValue
    {
        public string Name { get; set; }
        public object Data { get; set; }
        public RegistryValueKind Kind { get; set; }
    }
}
#endif